public class SinusoidalCurrent extends ParameterFramework implements IAppliedCurrent {
    private double amp , scaleX , shift , startT , stopT ;
    
    public String getParam( String name ) {
        if ( name.equals( "currentAmplitude" ) ) {
            return "" + amp ;
        } else if ( name.equals( "currentStart" ) ) {
            return "" + startT ;
        } else if ( name.equals( "currentStop" ) ) {
            return "" + stopT ;
        } else if ( name.equals( "currentPeriod" ) ) {
            return "" + ( 2 * Math.PI ) / scaleX ;
        } else if ( name.equals( "currentShift" ) ) {
            return "" + shift ;
            
        } else {
            return null ;
        }
    }
    
    public void setParam( String name , String data ) {
        double value ;
        
        // all parameters are doubles
        try {
            value = Double.parseDouble( data ) ;
        } catch ( NumberFormatException e ) {
            // not a double; ignore the set request
            return ;
        }
        if ( name.equals( "currentAmplitude" ) ) {
            amp = value ;
        } else if ( name.equals( "currentStart" ) ) {
            startT = value ;
        } else if ( name.equals( "currentStop" ) ) {
            stopT = value ;
        } else if ( name.equals( "currentPeriod" ) ) {
            scaleX = Math.PI * 2. / value ;
        } else if ( name.equals( "currentShift" ) ) {
            shift = value ;
        }
    }
    
    private void setupParams( ) {
        String [ ] params = { "currentStart" , "currentStop" , "currentAmplitude" , "currentPeriod" ,
        "currentShift" } ;
        parameterNames = params ;
    }
    
    public SinusoidalCurrent( double amp , double scaleX , double shift ) {
        setupParams( ) ;
        this.amp = amp ;
        this.scaleX = scaleX ;
        this.shift = shift ;
        startT = Double.NEGATIVE_INFINITY ;
        stopT = Double.POSITIVE_INFINITY ;
    }
    
    public String getName( ) {
        return "Sinusoidal Current" ;
    }
    
    public SinusoidalCurrent( double amp , double period , double shift , double startT , double stopT ) {
        setupParams( ) ;
        this.amp = amp ;
        this.scaleX = 2 * Math.PI / period ;
        this.shift = shift ;
        this.startT = startT ;
        this.stopT = stopT ;
    }
    
    public IAppliedCurrent clone( ) {
        SinusoidalCurrent c = new SinusoidalCurrent( amp , scaleX , shift ) ;
        c.setParam( getParams( ) ) ;
        return c ;
    }
    
    
    public double amount( double t ) {
        if ( t >= startT && t <= stopT ) {
            return amp * Math.sin( scaleX * t - shift ) ;
        } else {
            return 0 ;
        }
    }
}
